<?php

class Search_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getUrl() {
        $this->db->select('*');
        $this->db->from('short_url');
        $this->db->order_by("id", "DESC");
        $query = $this->db->get();
        //echo $this->db->last_query();
        return $query->result();
    }

    public function record_count() {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;
        $this->db->where('user_id', $id);
        $this->db->from('short_url');
        return $this->db->count_all_results();
        
        
        //return $this->db->count_all("short_url");
    }

    public function fetch_urls($limit, $start) {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;


        $this->db->limit($limit, $start);
        $this->db->select('short_url.id, short_url.user_id, short_url.short_url, short_url.long_url, short_url.create_time, COUNT(track_url.short_url) AS hits');
        $this->db->from('short_url');
        $this->db->join('track_url', 'short_url.short_url=track_url.short_url', 'left');
        $this->db->group_by('short_url.short_url');
        $this->db->where('short_url.user_id', $id);
        $this->db->order_by("short_url.id", "DESC");
        $query = $this->db->get();
       //echo $this->db->last_query();

       return  $query->result();
    }

    public function searchUrls($limit, $start) {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;
        
        $this->db->limit($limit, $start);
        $this->load->library('session');

        if ($_POST) {
            $this->session->set_userdata($_POST);
        }

        $data = $this->session->all_userdata();

        $long_url = $data['long_url'];
        $short_url = substr($data['short_url'], -6);
        $datepickerstart = $data['create_timestart'];
        $datepickerend = $data['create_timeend'];

        //$long_url = $this->input->post('long_url');
        //$this->session->set_userdata('url',$long_url);
        // $long_url = $this->input->post('long_url');
        // $short_url = $this->input->post('short_url');

        $datepickerstart = date("Y-m-d", strtotime($data['create_timestart']));
        $datepickerend = date("Y-m-d", strtotime($data['create_timeend'])) . '23:59:59';
        if ($long_url != '') {
            $this->db->like('short_url.long_url', $long_url);
        }
        if ($short_url != '') {
            $this->db->like('short_url.short_url', $short_url);
        }
        if ($datepickerstart != '1970-01-01') {
            if ($datepickerstart != $datepickerend) {
                $this->db->where('short_url.create_time >=', $datepickerstart);
                $this->db->where('short_url.create_time <=', $datepickerend);
            } else
                $this->db->like('short_url.create_time', $datepickerstart);
        }
        $this->db->select('short_url.id, short_url.short_url, short_url.long_url, short_url.create_time, COUNT(track_url.short_url) AS hits');
        $this->db->from('short_url');
        $this->db->join('track_url', 'short_url.short_url=track_url.short_url', 'left');
        $this->db->group_by('short_url.short_url');
        $this->db->where('short_url.user_id', $id);
        $this->db->order_by("short_url.id", "DESC");
        $query = $this->db->get();
        echo $this ->db-> last_query();
        return $query->result();
        
    }

    public function searchUrls1() {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;

        $this->load->library('session');

        if ($_POST) {
            $this->session->set_userdata($_POST);
        }
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;
        $data = $this->session->all_userdata();

        $long_url = $data['long_url'];
        $short_url = substr($data['short_url'], -6);
        $datepickerstart = $data['create_timestart'];
        $datepickerend = $data['create_timeend'];

        //$long_url = $this->input->post('long_url');
        //$this->session->set_userdata('url',$long_url);
        // $long_url = $this->input->post('long_url');
        // $short_url = $this->input->post('short_url');

        $datepickerstart = date("Y-m-d", strtotime($data['create_timestart']));
        $datepickerend = date("Y-m-d", strtotime($data['create_timeend'])) . '23:59:59';
        if ($long_url != '') {
            $this->db->like('short_url.long_url', $long_url);
        }
        if ($short_url != '') {
            $this->db->like('short_url.short_url', $short_url);
        }
        if ($datepickerstart != '1970-01-01') {
            if ($datepickerstart != $datepickerend) {
                $this->db->where('short_url.create_time >=', $datepickerstart);
                $this->db->where('short_url.create_time <=', $datepickerend);
            } else
                $this->db->like('short_url.create_time', $datepickerstart);
        }
        $this->db->where('short_url.user_id', $id);
        $this->db->select('short_url.id, short_url.short_url, short_url.long_url, short_url.create_time, COUNT(track_url.short_url) AS hits');
        $this->db->from('short_url');
        $this->db->join('track_url', 'short_url.short_url=track_url.short_url', 'left');
        $this->db->group_by('short_url.short_url');
        $this->db->where('short_url.user_id', $id);

        $query = $this->db->get();

        return $query->result();
    }

    function DeleteUrl($id) {
        $id = $_GET['id'];
        $this->db->delete('short_url', array('short_url' => $id));
        $this->db->delete('track_url', array('short_url' => $id));
        
        $this->session->set_flashdata('msg', 'Shorl URL SuccessFully Deleted');
       //$location = 'location:' . base_url() . "Searchurl?msg=Short URL => $id SuccessFully Deleted";
        redirect(base_url().'Searchurl');
    }

    function editUrl() {
        $id = $_GET['id'];
        $this->db->select('*');
        $this->db->where('id', $id);
        $query = $this->db->get('short_url');
        return $query->result();
    }

    function updateUrl($data) {
        $id = $this->input->post('id');
        $data = array('long_url' => $data['long_url'],);
        $this->db->where('id', $id);
        $this->db->update('short_url', $data);
        //  echo $this->db->last_query(); 

        $this->session->set_flashdata('msg', 'Long URL has been  updated successfully');
      
        redirect(base_url().'Searchurl');
    }

}

?>
